import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
"""
数据的可视化
    探索泰坦尼克灾难数据
"""


def man_and_mens_sector(X,Y):
    """
    用于绘制展示男女乘客比例的扇形图
    :return:
    """
    print('man%smen%s'%(X,Y))
    proportions =[X,Y]
    # 创建一个chart
    plt.pie(
        proportions,
        shadow=True, # 是否展示阴影
        colors=['red','green'],  # 颜色显示
        explode=(0.15,0),
        startangle=90,
        autopct='%1.1f%%'
    )
    plt.axis('equal')
    plt.title('Sex Proportion')
    plt.tight_layout()
    plt.show()


def fare_plt(data):
    """
    绘制创票展示船票Fare,与乘客年龄和性别的散点图
    :param object:
    :return:
    """
    lm = sns.lmplot(x='Age',y='Fare',data=data,hue='Sex',fit_reg=False)
    lm.set(title='Fare x Age') # 设置标题
    axes = lm.axes
    axes[0,0].set_ylim(-5,)
    axes[0,0].set_xlim(-5,85)
    plt.show()


def stritht_pic(df,binsVal):
    """
    船票价格直方图
    :param df:
    :param binsVal:
    :return:
    """
    plt.hist(df,bins=binsVal)
    plt.xlabel('Fare')
    plt.ylabel('Frequency')
    plt.title('Fare Payed Histrogram')
    plt.show()


def explore_taitan():
    # 1.导入数据
    path = '../Pandas_exercises/train.csv'
    # 2.将数据框命名为trainic
    trainic = pd.read_csv(path)
    # 3.将passengerId设置为索引
    trainic.set_index('PassengerId')
    # 4.绘制一个展示男女乘客比例的扇形图
    females = (trainic['Sex'] == 'female').sum()
    males = (trainic['Sex'] == 'male').sum()
    # man_and_mens_sector(females,males)
    # 5.绘制一个展示船票Fare,与乘客年龄和性别的散点图
    fare_plt(trainic)
    # 6.有多少人生还
    print('有多少人生还：\n',trainic.Survived.sum())
    # 7.绘制一个展示船票价格的直方图
    df = trainic.Fare.sort_values(ascending=False)
    binsVal = np.arange(0,600,10)
    stritht_pic(df,binsVal)
explore_taitan()